﻿using ContosoUniversity.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace ContosoUniversity.DAL
{
    public class SchoolContext : DbContext
    {
        /// <summary>
        /// The name of the connection string (which you'll add to the Web.config file later) is passed in to the constructor.
        /// </summary>
        public SchoolContext() : base("SchoolContext")
        {
        }

        public DbSet<Student> Students { get; set; }
        public DbSet<Enrollment> Enrollments { get; set; }
        public DbSet<Course> Courses { get; set; }

        public DbSet<Instructor> Instructors { get; set; }
        public DbSet<Department> Departments { get; set; }

        public DbSet<OfficeAssignment> OfficeAssignments { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            //防止数据库中的表名被多元化。如果没有这样处理的话，生成的表将会被命名为 Students，Courses 和 Enrollments。
            //现在，表名将会被命名为 Student，Course 和 Enrollment。开发者可以决定表名是否被多元化。这个教程使用单数模式，
            //但是重要的是你可以这行代码来选择喜欢的模式。
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            //  modelBuilder.Entity<Department>.HasRequired(d => d.Administrator).WithMany().WillCascadeOnDelete(false);
            modelBuilder.Entity<Course>()
                .HasMany(c => c.Instructors).WithMany(i => i.Courses)
                .Map(t => t.MapLeftKey("CourseID")
                .MapRightKey("InstructorID")
                .ToTable("CourseInstructor"));
        }
    }
}